package org.wattdepot.cli.commands;

import javax.xml.datatype.XMLGregorianCalendar;
import org.wattdepot.cli.UserCommandInterface;
import org.wattdepot.client.WattDepotClientException;
import org.wattdepot.util.tstamp.Tstamp;

/**
 * Contains methods for the carbon content command.
 * 
 * @author Jarret Mizo, Shaun Ramento
 * 
 */
public class CarbonContent extends UserCommandInterface implements UserCommands {

  @Override
  public String doCommand(String args) {
    String[] inputString = args.split(" ");
    String output = null;
    try {
      output = getCarbonContent(inputString[1], inputString[2], inputString[4]);
    }
    catch (WattDepotClientException e) {
      System.out.println("Carbon content error.");
    }
    return output;
  }

  /**
   * Computes the carbon content of the source.
   * 
   * @param sourceName The source name.
   * @param timeStamp The timestamp.
   * @param sampling The sampling-interval time.
   * @throws WattDepotClientException If there is an error.
   * @return The carbon content output.
   */
  public String getCarbonContent(String sourceName, String timeStamp, String sampling)
      throws WattDepotClientException {
    ValidateDay day = new ValidateDay();
    // gets the xml date of the string entered
    XMLGregorianCalendar xmlStartDate = day.validateDay(timeStamp);
    XMLGregorianCalendar xmlEndDate;
    double carbon;
    double energy;
    double carbonGenerated;
    int samplingInterval;
    // holds the string that will be displayed
    StringBuffer buffer = new StringBuffer(1024);

    // check if source with sourceName exists
    if (!sourceExists(sourceName)) {
      // exit method
      return "";
    }

    // checks if xmlDate was created if it is null, return to command prompt
    if (xmlStartDate == null) {
      return "";
    }

    // gets the sampling interval, returns error message if input 'sampling' is not a number
    try {
      samplingInterval = Integer.parseInt(sampling);
      if (0 >= samplingInterval || samplingInterval > 60) {
        return "Sampling Interval must be greater than 0 and less than 60";
      }
    }
    catch (Exception e) {
      return "Sampling Interval input is not a number.";
    }

    xmlEndDate = Tstamp.incrementHours(xmlStartDate, 1);

    // gets the carbon emmited and energy generated
    carbon = getClient().getCarbonEmitted(sourceName, xmlStartDate, xmlEndDate, samplingInterval);
    energy = getClient().getEnergyGenerated(sourceName, xmlStartDate, xmlEndDate, samplingInterval);

    // convert energy to MW
    energy = energy / 1000000;
    carbonGenerated = Math.round(carbon / energy);

    // buffer.append("Carbon: " + carbon + "\n");
    // buffer.append("Energy: " + energy + "\n");
    // buffer.append("Carbon Generated: " + carbonGenerated + "\n");
    
    //append the carbonGenerated
    buffer.append(carbonGenerated);
    buffer.append(" lbs CO2/MWh\n");
    return buffer.toString();
  }

}
